<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { deleteResidentialById, getResidentialList } from "@/api/community";
// import Database from "@iconify-icons/ri/database-2-line";
// import More from "@iconify-icons/ep/more-filled";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import { useDetail } from "@/views/residential/utils/hooks";
import { message } from "@/utils/message";
import router from "@/router";

const { toDetail } = useDetail();
defineOptions({
  name: "residentialList"
});

const form = reactive({
  name: "",
  code: ""
});
const formRef = ref();
const dataList = ref([]);
const loading = ref(true);
const deleteDialogVisible = ref(false);
const residentialData = ref({});

/** 初始化数据或搜索数据 */
async function onSearch() {
  loading.value = true;
  const { data } = await getResidentialList(dataList.current, form.name);
  dataList.value = data;
  setTimeout(() => {
    loading.value = false;
  }, 500);
}

/** 删除函数 */
async function deleteResidential() {
  console.log(residentialData.value.id);
  deleteResidentialById(residentialData.value.id).then(res => {
    message(res.msg, { type: "success" });
    onSearch();
  });
}

const resetForm = formEl => {
  if (!formEl) return;
  formEl.resetFields();
  onSearch();
};

onSearch();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="小区名称：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入小区名称"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
        <el-button
          type="success"
          @click="router.push({name:'residentialEdit'})"
        >添加小区
        </el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="dataList.records" table-layout="auto">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="name" label="小区名" />
      <el-table-column prop="address" label="小区地址" />
      <el-table-column prop="completion" label="交付时间" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="primary"
            size="small"
            @click="toDetail({ id: scope.row.id }, 'params')"
          >详情
          </el-button>
          <el-button
            type="danger"
            size="small"
            @click="
              deleteDialogVisible = true;
              residentialData = scope.row;
            "
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%">
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="dataList.total"
        :page-count="dataList.current"
      />
    </div>
    <!--    处理数据的弹窗-->
    <el-dialog v-model="deleteDialogVisible" title="提醒" align-center>
      <span>确认要删除【{{ residentialData.name }}】小区吗？</span>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="deleteDialogVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="
              deleteResidential();
              deleteDialogVisible = false;
            "
          >
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}

.main-content {
  margin: 24px 24px 0 !important;
}

.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
